//主循环,慢任务
class mainLoop
{
    static go()
    {
        setInterval(() => {
           this.doLoop() 
        }, 1000);
    }

    //启动一个循环任务，当返回false表示结束任务
    static startLoop(loopFunc)
    {
        this.mLoopList.push(loopFunc);
    }

    static doLoop()
    {
        if (this.mLoopList.length < 1) return;

        var list = [];
        var delList = [];
        for (var i = 0; i < this.mLoopList.length; i++) list.push(this.mLoopList[i]);
        for(var i=0;i<list.length;i++)
        {
            try
            {
                if (!list[i]()) delList.push(list[i])
            }catch(msg)
            {
                console.error(msg)
            }
        }

        for(var i=0;i<delList.length;i++)
        {
            var currFunc = delList[i]
            for (var j = 0; j < this.mLoopList.length; j++) 
            {
                if(this.mLoopList[j] == currFunc)
                {
                    this.mLoopList.splice(j,1)
                    break
                }
            }
        }
    }
    static mLoopList = [];
}
export{mainLoop}